﻿
/********************************       Ajax Form Validation        ****************************************/
/// <reference path="JQuery/jquery-1.2.6.min-vsdoc.js" />
$(document).ready(function () {
    $(".ajaxSubmitButton").click(function () {

        var validateFormURL = "";
        var status = false;
        var form = $(".form"); //form
        var serializedForm = form.serialize(); //serialized form
        $("input[type=text]").removeClass("error"); // set the general css
        $("input[type=password]").removeClass("error"); // set the general css
        $("input[type=text]").addClass("TextBox");
        $("input[type=password]").addClass("TextBox");

        //
        var editPosition = document.URL.search("Edit");

        if (editPosition != -1) {
            validateFormURL = document.URL.substring(0, editPosition);

        }

        $.post(validateFormURL + "ValidateForm", serializedForm, function (resul) { //Validate form using Ajax
            try {
                var response = eval("(" + resul + ")"); //Eval the Json Value                
                if (response.length == 0) { //there is no error
                    $.post($(".form").attr("action"), serializedForm, function (resul) { // call the action in the form and send the parameters

                        var replaceActionInPosition = -1;
                        var action;

                        if (document.URL.search("Edit") != -1) {
                            replaceActionInPosition = document.URL.search("Edit");
                            action = "Edit";
                        }
                        else {
                            replaceActionInPosition = document.URL.search("Create");
                            action = "Create";
                        }
                        window.location.href = document.URL.replace(document.URL.substring(replaceActionInPosition, document.URL.length), action);

                    }); // $post
                }
                else { // There is some errors

                    for (var i = 0; i < response.length; i++) {
                        $("#" + response[i].Field + "Message").html(response[i].Message);
                    }
                }
            } catch (err) {
                alert("Ocorreu um erro.\n " + err);
            }
        });

        return status;
    });
    /********************************    Code that submits form to validate so then returns to server-side     ****************************************/
    $(".submitButton").click(function () {
        var validateFormURL = "";
        var status = false;
        var form = $(".form"); //form
        var serializedForm = form.serialize(); //serialized form
        $("input[type=text]").removeClass("error"); // set the general css
        $("input[type=password]").removeClass("error"); // set the general css
        $("input[type=text]").addClass("TextBox");
        $("input[type=password]").addClass("TextBox");

        //
        var editPosition = document.URL.search("Edit");

        if (editPosition != -1) {
            validateFormURL = document.URL.substring(0, editPosition);

        }
        $.post(validateFormURL + "ValidateForm", serializedForm, function (resul) { //Validate form using Ajax
            try {
                var response = eval("(" + resul + ")"); //Eval the Json Value
                if (response.length == 0) { //there is no error
                    form.submit();
                    // Return true to submit form to its original actions
                }
                else { // There is some errors

                    for (var i = 0; i < response.length; i++) {
                        $("#" + response[i].Field + "Message").html(response[i].Message);
                    }
                }
            } catch (err) {
                alert("Ocorreu um erro.\n " + err);
            }
        });

        return status;
    });

});         // close load scripts


/********************************       MasterPage's Scripts      ****************************************/

function Start() {
    loadStyles();
}





/********************************       Style Scripts        ****************************************/

function loadStyles() {

    $("input[type=text]").addClass("TextBox");
    $("input[type=password]").addClass("TextBox");
    $("label").addClass("Textboxlabel");
    $("#main").corner("sc:#99CC00");


    /*  Calendar */

    $(".TextBoxDate").datepicker({
        changeMonth: true,
        changeYear: true,
        nextText: "Próximo",
        prevText: "Anterior",
        dateFormat: "dd/mm/yy",
        monthNamesShort: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
        dayNamesMin: ['D', 'S', 'T', 'Q', 'Q', 'S', 'S'],
        dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado']
        , defaultDate: new Date(1990, 1 - 1, 1)
    }).attr("readonly", "true");

    $(".TodayDate").datepicker({
        changeMonth: true,
        changeYear: true,
        nextText: "Próximo",
        prevText: "Anterior",
        dateFormat: "dd/mm/yy",
        monthNamesShort: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
        dayNamesMin: ['D', 'S', 'T', 'Q', 'Q', 'S', 'S'],
        dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado']
        , defaultDate: new Date()
    }).attr("readonly", "true");
}

      
